
// Definition for a Node.
function Node(val, next, random) {
   this.val = val;
   this.next = next;
   this.random = random;
};


/**
 * @param {Node} head
 * @return {Node}
 */
var copyRandomList = function(head) {
  if(!head) return null;
  let map = new Map(), temp = head;
  while(temp) {
    let node = new Node(temp.val, null, null);
    map.set(temp, node);
    temp = temp.next;
  }
  temp = head;
  target = map.get(head);
  while(temp) {
    if(temp.next) target.next = map.get(temp.next);
    if(temp.random) target.random = map.get(temp.random);
    target = target.next;
    temp = temp.next;
  }
  return map.get(head);
};
